<template>
	<view class="back-view" @touchmove.stop.prevent="moveHandle">
		<view class="view-background"></view>
		<view class="white-view-back">
			<view class="top-back">
				<view class="top-name">{{pageTitle}}</view>
				<view class="close-back" @click="closeBtnAction">
					<image class="close-img" src="../../../static/close.png"></image>
				</view>
			</view>
			<view class="top-line"></view>
			<scroll-view class="center-back" scroll-y="true">
				<uni-forms ref="baseForm" labelWidth="90" :modelValue="baseFormData">
					<uni-forms-item label="工作日期：" required>
						<view class="item-back">
							<uni-easyinput v-model="baseFormData.workDate" placeholder="请填写(格式:2022-01-01)" />
						</view>
					</uni-forms-item>
					<uni-forms-item label="工作内容：" required>
						<uni-easyinput type="textarea" v-model="baseFormData.workContent" placeholder="请填写" />
					</uni-forms-item>
					<uni-forms-item label="工作时长：" required>
						<view class="item-back">
							<uni-easyinput type="digit" v-model="baseFormData.workDuration" placeholder="请填写" />
							<view class="item-hour">小时</view>
							<view class="item-hour-des">（指人均工作时长）</view>

						</view>
					</uni-forms-item>
					<uni-forms-item label="工作律师：" required>
						<view class="law-item-back">
							<input @input="onLawyerKeyInput" class="selectLaw" @focus="startLawyerEditAction"
								v-model="baseFormData.code1" placeholder="输入律师姓名/手机号/工作组"></input>
							<view v-if="lawyerShowMask" class="selectLaw-list">
								<scroll-view class="pop-content-back" scroll-y="true">
									<view class="pop-content-list" v-for="(item,index) in lawyerArr" :key="index"
										@click="clickLawyerPopIremAction(item)">
										<image class="pop-content-img" :src="item.account.avatarPic"></image>
										<view class="pop-content-name">{{item.account.realName}}</view>
										<view class="pop-content-phone">{{item.account.mobile}}</view>
									</view>
								</scroll-view>
							</view>
						</view>
						<view class="mask-view" v-if="lawyerShowMask" @tap="tapLawyerMask"></view>
						<view class="customer-item-back">
							<view class="customer-item" v-for="(item,index) in lawyerSelectedArr" :key="index">
								<view class="customer-back">
									<image class="customer-img" :src="item.account.avatarPic"></image>
									<view class="customer-name">{{item.account.realName}}</view>
									<view class="customer-phone">{{item.account.mobile}}</view>
									<image class="customer-close" @click="clickLawyerDelAction(index)"
										src="../../../static/phone/guanbi1.png"></image>
								</view>
							</view>
						</view>
					</uni-forms-item>
				</uni-forms>
			</scroll-view>
			<view class="bottom-back">
				<view class="bottom-btn bottom-btn-cancel" @click="closeBtnAction">取消</view>
				<view class="bottom-btn" @click="submitAction">确认</view>
			</view>
		</view>
	</view>
</template>

<script>
	import dataselect from "@/components/data-select/dataselect.vue"
	import selectxy from "@/components/select-cy/select-cy.vue"
	let app = ''

	export default {
		components: {
			dataselect,
			selectxy
		},
		props: {
			lawyerLists: {
				type: Array,
				default: {},
			},
			selectItem: {
				type: Object,
				default: {}
			},
			editType: {
				type: String,
				default: 'add',
			},

		},
		data() {
			return {
				role: '', //当前登录角色
				roleUrl: '',
				pageTitle: "新增记录",
				baseFormData: {
					caseId: "",
					createAt: "",
					createBy: "",
					createTime: "",
					id: "",
					workContent: "",
					workDate: "",
					workDuration: 0,
				},

				lawyerList: [], //常用律师
				lawyerArr: [], //常用律师去掉已选
				lawyerSelectedArr: [], //常用律师已选择
				lawyerShowMask: false, //常用律师
			}
		},
		mounted() {
			var _this = this;
			app = getApp()
			_this.role = app.globalData.role
			_this.roleUrl = _this.$roleUrl({
				tab: _this.role
			})
			this.requestLawyerData();
			if (this.editType == 'edit') {
				this.pageTitle = '编辑记录';
				this.requestInfoData();
				this.lawyerList = this.lawyerLists;
			}
		},
		methods: {
			//信息网络请求
			async requestInfoData() {
				let _this = this
				const res = await this.$myRequest({
					url: `/${_this.roleUrl}/caseWorkLog/` + this.selectItem.id,
					method: 'GET',
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					_this.baseFormData = res.data.data;


					if (res.data.data.workDate) { //日期去掉时分秒
						var times = res.data.data.workDate.split(' ');
						if (times.length > 0) {
							_this.$set(_this.baseFormData, 'workDate', times[0])
						}
					}


					//常用律师
					var lawyerIDs = _this.baseFormData.lawyers;
					for (var i = 0; i < _this.lawyerList.length; i++) {
						var value = _this.lawyerList[i];
						for (var j = 0; j < lawyerIDs.length; j++) {
							var item = lawyerIDs[j];
							if (item.account.id == value.accId && item.status.code == '1') {
								_this.lawyerSelectedArr.push(value)
							}
						}
					}
				}
			},
			//保存
			async submitAction() {
				let _this = this
				var jsInt = /^[0-9]+.?[0-9]*$/; //判断字符串是否为数字
				if (!this.baseFormData.workDate) {
					uni.showToast({
						title: '请填写工作日期',
						icon: 'none'
					})
					return;
				}
				if (!this.baseFormData.workContent) {
					uni.showToast({
						title: '请填写工作内容',
						icon: 'none'
					})
					return;
				}
				if (!this.baseFormData.workDuration) {
					uni.showToast({
						title: '请填写工作时长',
						icon: 'none'
					})
					return;
				}
				if (!jsInt.test(this.baseFormData.workDuration)) {
					uni.showToast({
						title: '工作时长必须为数字',
						icon: 'none'
					})
					return;
				}
				if (!_this.lawyerSelectedArr.length) {
					uni.showToast({
						title: '请选择律师',
						icon: 'none'
					})
					return;
				}

				var parms = {
					'id': this.baseFormData.id,
					'workContent': this.baseFormData.workContent,
					'workDate': this.baseFormData.workDate,
					'workDuration': this.baseFormData.workDuration,
				}

				//常用律师
				var lawyerArr = [];
				for (var i = 0; i < _this.lawyerSelectedArr.length; i++) {
					var item = _this.lawyerSelectedArr[i];
					lawyerArr.push(item.id);
				}
				if (lawyerArr) {
					_this.$set(parms, 'lawyers', lawyerArr);
				}
				console.log(parms)
				var url = `/${_this.roleUrl}/caseWorkLog`;

				const res = await this.$myRequest({
					url: url,
					method: _this.editType == 'edit' ? 'PUT' : 'POST',
					data: parms
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					uni.showToast({
						title: _this.editType == 'edit' ? '编辑成功' : '添加成功'
					})
					_this.$emit("successBack");
					_this.closeBtnAction()
				}
			},

			//常用律师：网络请求
			async requestLawyerData() {
				let _this = this
				var url = "/admin/lawyer/selector";
				let parmas = {size:1000,status:'1'};
				if (_this.baseFormData.code1) {
					this.$set(parmas, 'keyword', _this.baseFormData.code1)
				}
				const res = await this.$myRequest({
					url: url,
					method: 'POST',
					data: parmas,
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					_this.lawyerList = res.data.data;
					_this.refreshLawyerDataAction();
				}
			},
			//常用律师：开始输入
			startLawyerEditAction(e) {
				console.log(e)
				this.lawyerShowMask = true
			},
			//常用律师：输入
			onLawyerKeyInput(event) {
				console.log(event.target.value)
				this.requestLawyerData()
			},
			//常用律师：隐藏
			tapLawyerMask() {
				this.lawyerShowMask = false
			},
			//常用律师：选择
			clickLawyerPopIremAction(item) {
				this.lawyerShowMask = false;
				this.lawyerSelectedArr.push(item);
				this.refreshLawyerDataAction();
			},

			//常用律师：刷新列表
			refreshLawyerDataAction() {
				this.lawyerArr = [];
				for (var i = 0; i < this.lawyerList.length; i++) {
					var value = this.lawyerList[i];
					var hasSelect = false;
					for (var j = 0; j < this.lawyerSelectedArr.length; j++) {
						var tem = this.lawyerSelectedArr[j];
						if (value.account.id == tem.account.id) {
							hasSelect = true;
						}
					}
					if (!hasSelect) {
						this.lawyerArr.push(value);
					}
				}
			},
			//常用律师：删除已选用户
			clickLawyerDelAction(index) {
				this.lawyerSelectedArr.splice(index, 1);
				this.refreshLawyerDataAction();
			},

			clickShowinfoAction() {
				this.showInfo = !this.showInfo
			},
			clickShowTickAction() {
				this.showTick = !this.showTick
			},
			typeChange(e) {
				console.log("e:", e);
			},

			teamTagChange(e) {
				console.log("e:", e);
			},

			closeBtnAction() {
				this.$emit('addWorkTimeClose')
			},
			moveHandle() {
				return true
			},

		},
	}
</script>

<style>
	.back-view {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 19;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.view-background {
		position: absolute;
		width: 100%;
		height: 100%;
		z-index: 20;
		background-color: rgba(0, 0, 0, 0.3);
	}


	.white-view-back {
		width: 552px;
		height: 491px;
		padding: 24px;
		max-height: 491px;
		background-color: white;
		margin-top: -70px;
		border-radius: 4px;
		z-index: 21;
		position: absolute;
	}

	.top-back {
		display: flex;
		justify-content: space-between;
	}

	.top-name {
		font-size: 18px;
		font-weight: normal;
		color: #333333;
	}

	.close-back {
		cursor: pointer;
		width: 20px;
		height: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.close-img {
		width: 12px;
		height: 12px;
	}

	.top-line {
		height: 1px;
		background: #EEEEEE;
		width: 100%;
		margin-top: 20px;
		margin-bottom: 20px;
	}

	.top-line1 {
		margin-top: 0px;
	}

	.bottom-back {
		bottom: 0px;
		width: 100%;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		position: absolute;
		height: 60px;
		right: 24px;
	}

	.bottom-btn {
		cursor: pointer;
		width: 104px;
		height: 32px;
		background: #2DA9C5;
		border-radius: 2px;
		color: #FFFFFF;
		font-size: 14px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.bottom-btn-cancel {
		background: #FFFFFF;
		border: 1px solid #EEEEEE;
		color: #333333;
		margin-right: 10px;
	}

	/* 
中间高度=总高 491- 20-21 - 20-60 + 14
 */
	.center-back {
		width: 100%;
		max-height: 384px;
		height: 384px;

	}


	.customer-item {
		display: flex;
		margin-top: 8px;
		align-items: center;
	}

	.customer-back {
		min-width: 168px;
		height: 44px;
		background: #E8F7FB;
		border-radius: 4px;
		display: flex;
		align-items: center;
	}

	.customer-img {
		width: 20px;
		height: 20px;
		border-radius: 50%;
		margin-left: 8px;
	}

	.customer-name {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 2px;
	}

	.customer-phone {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 4px;
	}

	.customer-close {
		width: 16px;
		height: 16px;
		margin-right: 2px;
		margin-left: auto;
	}

	.customer-author {
		font-size: 14px;
		font-weight: 400;
		color: #999999;
		margin-left: 16px;
		display: flex;
	}

	.customer-author2 {
		color: #2DA9C5;
		margin-left: 16px;
	}

	.info-show {
		font-size: 12px;
		font-weight: 400;
		color: #2DA9C5;
		display: flex;
		height: 40px;
		width: 60px;
		align-items: center;
	}

	.info-show-img {
		width: 12px;
		height: 12px;
		margin-left: 4px;

	}

	.info-list-back {
		margin-left: -70px;
		width: 300px;
	}

	.item-back {
		width: 400px;
		display: flex;
		align-items: center;
	}

	.item-back4 {
		width: 250px;
	}

	.bottom-des {
		font-size: 14px;
		font-weight: 400;
		color: #999999;
		margin-bottom: 10px;
	}

	.law-item-back {
		background-color: white;
		width: 300px;
	}

	.selectLaw {
		font-size: 14px;
		border: 1px solid #DCDFE6;
		border-radius: 4px;
		padding: 6px 10px;
		position: relative;
		margin-left: 0px;
		flex-direction: row;
		align-items: center;
		z-index: 100000;
		background-color: white;
	}

	.selectLaw-list {
		margin-top: 2px;
		position: absolute;
		width: 300px;
		height: 180px;
		z-index: 100010;
		background-color: white;
		box-shadow: 0px 2px 8px 0px rgba(0, 0, 0, 0.2000);
		border-radius: 2px;
	}

	.pop-content-back {
		margin-left: 20px;
		width: 280px;
		height: 100%;
	}

	.pop-content-list {
		display: flex;
		align-items: center;
		margin-bottom: 10px;
		margin-top: 10px;
	}

	.pop-content-img {
		width: 20px;
		min-width: 20px;
		height: 20px;
		border-radius: 50%;
	}

	.pop-content-name {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 6px;
	}

	.pop-content-phone {
		font-size: 12px;
		font-weight: 400;
		color: #666666;
		margin-left: 10px;
	}

	.mask-view {
		position: absolute;
		width: 100vw;
		height: 400px;
		z-index: 9999;
		background-color: rgba(0, 0, 0, 0.0);
		top: -100px;
		left: 0;
		right: 0;
		bottom: 0;
	}

	.item-hour {
		margin-left: 10px;
	}

	.item-hour-des {
		margin-left: 20px;
		font-size: 13px;
		color: #999999;
	}
</style>
